package com.ruoyi.management.service.impl;

import com.ruoyi.management.service.InvoiceService;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Service
public class invoiceServiceImpl implements InvoiceService {
    @Value("${tencent.cloud.secret-id}")
    private String secretId;

    @Value("${tencent.cloud.secret-key}")
    private String secretKey;

    @Value("${uploadfpUrl}")
    private String uploadfpUrl;

    @Value("${uploadsqUrl}")
    private String uploadsqUrl;

    @Value("${uploadsqUrlFpqjz}")
    private String uploadsqUrlFpqjz;
    @Override
    public String recognizeGeneralInvoice(String base64Image) {
        try {
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            OcrClient client = new OcrClient(cred, "ap-guangzhou", clientProfile);
            RecognizeGeneralInvoiceRequest req = new RecognizeGeneralInvoiceRequest();
            req.setImageBase64(base64Image);
            RecognizeGeneralInvoiceResponse resp = client.RecognizeGeneralInvoice(req);
//            return handleResponse(resp);
            return AbstractModel.toJsonString(resp);
        } catch (Exception e) {
            e.printStackTrace();
            return "识别失败";
        }
    }

    @Override
    public String saveLocalfile(MultipartFile file,String typefile){
        try {
            String urlfile;
            if(typefile == "ccsq"){
                urlfile=uploadsqUrl;
            }else{
                urlfile=uploadfpUrl;
            }
            Path dir = Paths.get(urlfile);
            if (!Files.exists(dir)) Files.createDirectories(dir);
            Path filePath = dir.resolve(file.getOriginalFilename());
            Files.write(filePath, file.getBytes());
            System.out.println("name:" +  filePath.getFileName());
            return filePath.getFileName().toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
//    public String saveLocalFilemultiple(MultipartFile[] files){
//        try {
//            Path dir = Paths.get(uploadfpUrl);
//            for()
//            if (!Files.exists(dir)) Files.createDirectories(dir);
//            Path filePath = dir.resolve(file.getOriginalFilename());
//            Files.write(filePath, file.getBytes());
//            return filePath.getFileName().toString();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
    @Override
    public String recognizeTableOCR(String base64Image){
        try {
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            OcrClient client = new OcrClient(cred, "ap-guangzhou", clientProfile);
            RecognizeTableAccurateOCRRequest req =  new RecognizeTableAccurateOCRRequest();
            req.setImageBase64(base64Image);
            RecognizeTableAccurateOCRResponse resp = client.RecognizeTableAccurateOCR(req);
//            return handleResponse(resp);
            return AbstractModel.toJsonString(resp);
        } catch (Exception e) {
            e.printStackTrace();
            return "识别失败";
        }
    }

    @Override
    public String saveLocalfilefp(MultipartFile file){
        try {
            String urlfile=uploadsqUrlFpqjz;
            Path dir = Paths.get(urlfile);
            if (!Files.exists(dir)) Files.createDirectories(dir);
            Path filePath = dir.resolve(file.getOriginalFilename());
            Files.write(filePath, file.getBytes());
            return uploadsqUrlFpqjz+'/' +filePath.getFileName().toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
